const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')
const db = low(adapter)

let hasPosts = db.has('posts').value()

if (!hasPosts) {
// Set some defaults (required if your JSON file is empty)
db.defaults({ posts: [], user: {}, count: 0 })
  .write()
}

exports.addPost = function(obj) {
  db.get('posts')
  .push(obj)
  .write()
}

exports.getDB = function() {
  return db
}